package com.gao.controller;

import com.gao.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Projectname SSM
 * @Filename test
 * @Author an
 * @Data 2022/8/5 18:11
 * @Description TODO
 */
@Controller
public class AjaxController {

    @RequestMapping("/test/ajax")
    public void testajax(Integer id, @RequestBody Map<String,Object> map, HttpServletResponse response) throws IOException {
        System.out.println("id:"+id);
        System.out.println(map);
//        System.out.println("requestBody:"+requestBody);
        response.getWriter().write("hello,ajax");
    }

    @RequestMapping("/test/RequestBody/json")
    public void testRequestBody(@RequestBody User user,HttpServletResponse response) throws IOException {
        System.out.println(user);
        response.getWriter().write("hello,RequestBody");
    }

    @RequestMapping("/test/ResponseBody")
    @ResponseBody
    public String testResponseBody(){
        return "success";
    }

    @RequestMapping("/test/ResponseBody/json")
    @ResponseBody
    public User testResponseBodyJson(){
        User user = new User(1001,"admin1","123456",23,"男");
        return user;
    }

    //响应浏览器map集合
//    @RequestMapping("/test/ResponseBody/json")
//    @ResponseBody
//    public Map<String, Object> testResponseBodyJson2(){
//        User user1 = new User(1001,"admin1","123456",23,"男");
//        User user2 = new User(1002,"admin2","123456",23,"男");
//        User user3 = new User(1003,"admin3","123456",23,"男");
//        Map<String, Object> map = new HashMap<>();
//        map.put("1001", user1);
//        map.put("1002", user2);
//        map.put("1003", user3);
//        return map;
//    }
}
